package com.producer.controller;

import com.producer.entity.User;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class TestController {
    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @GetMapping("/test")
    public String test1() {
        // 发送字符串
        rocketMQTemplate.syncSend("topic_family", "Hello, World!");
        // 同步发送
        rocketMQTemplate.convertAndSend("topic_family", "Hello, World!");
        User user = new User();
        user.setUserAge((byte) 18);
        user.setUserName("Kitty");
        // 发送对象
        rocketMQTemplate.syncSend("topic_family", user);
         // 发送spring 消息
        rocketMQTemplate.syncSend("topic_family", MessageBuilder.withPayload(
                user).setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON_VALUE).build());
        return "success";
    }
}
